package com.lanyuan.a.controller;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.lanyuan.a.atest.UserFormMap;
import com.lanyuan.a.bean.TreeObject;
import com.lanyuan.a.bean.User;
import com.lanyuan.a.service.TreeObjectService;
import com.lanyuan.a.service.UserService;
import com.lanyuan.controller.index.BaseController;
import com.lanyuan.entity.ResFormMap;
import com.lanyuan.util.Common;
import com.lanyuan.util.TreeUtil;

@Controller
@RequestMapping("/")
public class BackgroundController extends BaseController {

	@Autowired
	UserService userService;
	@Autowired
	TreeObjectService treeObjectService;
	/**
	 * @return
	 */
	@RequestMapping(value = "login", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
	public String login(HttpServletRequest request) {
		request.removeAttribute("error");
		return "/login";
	}

	@RequestMapping(value = "login", method = RequestMethod.POST, produces = "text/html; charset=utf-8")
	public String login(Model model , String username, String password, HttpServletRequest request) {
		try {
			if (!request.getMethod().equals("POST")) {
				request.setAttribute("error", "支持POST方法提交！");
			}
			if (Common.isEmpty(username) || Common.isEmpty(password)) {
				request.setAttribute("error", "用户名或密码不能为空！");
				return "/login";
			}
			User user = new User(username,password);
			User currentUser = userService.findUserByNameAndPassword(user);
			System.out.println(currentUser);
			if(currentUser == null){
				request.setAttribute("error", "用户或密码不正确！");
				return "/login";
			}
			request.getSession().setAttribute("user", currentUser);
//			request.setAttribute("accountName", currentUser.getAccountname());
			model.addAttribute("user", currentUser);
			return index(model,currentUser.getRoot());
		}catch(Exception e){
			e.printStackTrace();
			request.setAttribute("error", "登录时出现错误！");
			return "/login";
		}
//		return "/index";
//		return "redirect:index.shtml";
	
	}

	@RequestMapping("myindex")
	public String myindex(Model model , HttpServletRequest request) throws Exception {
		User user = (User)request.getSession().getAttribute("user");
		
		List<TreeObject> list = treeObjectService.findAllList(user.getRoot());
		TreeUtil treeUtil = new TreeUtil();
		List<TreeObject> ns = treeUtil.getChildTreeObjects(list, 0);
		model.addAttribute("list", ns);
		return "/index";
	}
	
	public String index(Model model,int root) throws Exception {
	
		List<TreeObject> list = treeObjectService.findAllList(root);
		TreeUtil treeUtil = new TreeUtil();
		for(TreeObject to : list){
			System.out.println(to);
		}
		
		List<TreeObject> ns = treeUtil.getChildTreeObjects(list, 0);
		model.addAttribute("list", ns);
		// 登陆的信息回传页面
		return "/index";
	}
	

	@RequestMapping("menu")
	public String menu(Model model) {
		return "/framework/menu";
	}

	

	@RequestMapping(value = "logout", method = RequestMethod.GET)
	public String logout(HttpServletRequest request) {
		 // 清除session
        Enumeration<String> em = request.getSession().getAttributeNames();
        while (em.hasMoreElements()) {
            request.getSession().removeAttribute(em.nextElement().toString());
        }
        request.getSession().invalidate();
		return "redirect:login.shtml";
	}

	@RequestMapping(value = "regist", method = RequestMethod.POST, produces = "text/html; charset=utf-8")
	public String regist(String username, String password, HttpServletRequest request) {
		try {
			if (!request.getMethod().equals("POST")) {
				request.setAttribute("error", "支持POST方法提交！");
			}
			if (Common.isEmpty(username) || Common.isEmpty(password)) {
				request.setAttribute("error", "用户名或密码不能为空！");
				return "/login";
			}
			long count;
			try{
				count = userService.findUserByName(username);
			}catch(Exception e){
				count = 1;
			}
			System.out.println(count);
			if(count > 0){
				request.setAttribute("error", "用户名已存在");
				return "/login";
			}
			request.setAttribute("accountName", username);
			request.setAttribute("error", "return");
		}catch(Exception e){
			e.printStackTrace();
			request.setAttribute("error", "注册时出现错误！");
			return "/login";
		}
		return "/login";
	}


}
